<?php
namespace Home\Controller;
use Home\Core\CommandSDK;
use Think\Controller;

class LoginController extends HttpController
{
    public function __construct()
    {
        parent::__construct();
    }
    public function login()
    {
        $this->display();
    }
    public function login_post(){
        $data = I('post.');
        $password=$data['pswd']*1;
        $param['username'] = $data['username'];
        $param['pswd'] = substr(md5("$password"),0,16);
        $sdk = new CommandSDK();
        $return = $this->http_request($sdk::Login,$param,'GET');
        $array = json_decode(json_encode($return), true);
        $array['msg']=$sdk::error_code[$return->result];
        $userrole = $return->userrole;
        $encodekey=$return->encodekey;
        $signature=$return->signature;
        $ip=$return->vlinkerip;
        $port=$return->vlinkerport;
        $tel = $return->tel;
        $companyname=$return->companyname;
        $uid = $return->accountid;
        $contact=$return->contact;
        $session['encodekey'] = $encodekey;
        $session['signature'] = $signature;
        $session['ip'] = $ip;
        $session['port'] = $port;
        $session['companyname'] = $companyname;
        $session['contact']=$contact;
        $session['userrole'] = $userrole;
        $session['tel'] = $tel;
        $session['userid'] = $uid;
        $session['username'] = $data['userName'];
        if($return->result == 0){
            if($data['rember'] === 'true'){   // 默认有效期半年
                setcookie("user_info", base64_encode(http_build_query($session)), time()+3600*24*180,'/');
            } else {
                setcookie("user_info", base64_encode(http_build_query($session)), 0,'/');
            }
            if($userrole == '3'){  // 普通用户
                $array['result'] = 200;
                $array['msg'] = '普通用户不能登陆';
            }
        }
        $this->ajaxReturn($array);
    }
    public function logout(){
         cookie('user_info',null);
         session('AreaId',null);
         $this->redirect('Login/login');
    }
}